﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.OracleClient;
namespace BenefitHelper.Data.ServerData
{
    public class T_Account
    {
        /// <summary>
        /// 编号
        /// </summary>
        public int Id { get; set; }
        /// <summary>
        /// 客户编号
        /// </summary>
        public int CustomeId { get; set; }

        /// <summary>
        /// 登录账号
        /// </summary>
        public string LoginAccount { get; set; }

        /// <summary>
        /// 登录密码
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        /// 动态密码
        /// </summary>
        public string DynamicPassword { get; set; }


        /// <summary>
        /// 持仓量上限(针对所有产品)
        /// </summary>
        public int TotalPositions { get; set; }


        /// <summary>
        /// 持仓保证金上限(针对所有产品)
        /// </summary>
        public double TotalPosCost { get; set; }

        /// <summary>
        /// 最大撤单手数(针对所有合约)
        /// </summary>
        public int MaximumWithdrawals { get; set; }
        /// <summary>
        /// 是否被删除
        /// </summary>
        public int Isdeleted { get; set; }
        /// <summary>
        /// 追保风险度
        /// </summary>
        public double AppendMarginRate { get; set; }
        /// <summary>
        /// 强平风险度
        /// </summary>
        public double ForceClose { get; set; }
        /// <summary>
        /// 获取所有平台的虚拟账户
        /// </summary>
        /// <returns></returns>
        public List<T_Account> GetList()
        {
            List<T_Account> accounts = new List<T_Account>();
            string sql = "select * from T_Account Order by LoginAccount ";
            OracleDataReader sdr = Common.ADO.Tools.OracleHelper.ExecuteReader(sql);
            while (sdr.Read())
            {
                T_Account c = new T_Account();
                //if (sdr["AppendMarginRate"] != null)
                //{
                //    c.AppendMarginRate = Convert.ToDouble(sdr["AppendMarginRate"]);
                //}
                c.CustomeId = Convert.ToInt32(sdr["CustomeId"]);
                c.DynamicPassword = Convert.ToString(sdr["DynamicPassword"]);
                //c.ForceClose = Convert.ToDouble(sdr["ForceClose"]);
                c.Id = Convert.ToInt32(sdr["Id"]);
                c.Isdeleted = Convert.ToInt32(sdr["Isdeleted"]);
                c.LoginAccount = Convert.ToString(sdr["LoginAccount"]);
                c.MaximumWithdrawals = Convert.ToInt32(sdr["MaximumWithdrawals"]);
                c.Password = sdr["Password"].ToString();
                c.TotalPosCost = Convert.ToDouble(sdr["TotalPosCost"]);
                c.TotalPositions = Convert.ToInt32(sdr["TotalPositions"]);
                accounts.Add(c);
            }
            sdr.Close();
            return accounts;
        }

        /// <summary>
        /// 获取所有平台的虚拟账户
        /// </summary>
        /// <returns></returns>
        public List<T_Account> GetUnDeletedAccounts()
        {
            List<T_Account> accounts = new List<T_Account>();
            string sql = "select * from T_Account where Isdeleted=0 Order by LoginAccount";
            OracleDataReader sdr = Common.ADO.Tools.OracleHelper.ExecuteReader(sql);
            while (sdr.Read())
            {
                T_Account c = new T_Account();
                c.AppendMarginRate = Convert.ToDouble(sdr["AppendMarginRate"]);
                c.CustomeId = Convert.ToInt32(sdr["CustomeId"]);
                c.DynamicPassword = Convert.ToString(sdr["DynamicPassword"]);
                c.ForceClose = Convert.ToDouble(sdr["ForceClose"]);
                c.Id = Convert.ToInt32(sdr["Id"]);
                c.Isdeleted = Convert.ToInt32(sdr["Isdeleted"]);
                c.LoginAccount = Convert.ToString(sdr["LoginAccount"]);
                c.MaximumWithdrawals = Convert.ToInt32(sdr["MaximumWithdrawals"]);
                c.Password = sdr["Password"].ToString();
                c.TotalPosCost = Convert.ToDouble(sdr["TotalPosCost"]);
                c.TotalPositions = Convert.ToInt32(sdr["TotalPositions"]);
                accounts.Add(c);
            }
            sdr.Close();
            return accounts;
        }
        /// <summary>
        /// 判断分析系统的账户数量是否和平台一致
        /// </summary>
        /// <returns></returns>
        public bool AccountCountIsSameToBenefit(DB.DBManager db)
        {
            bool accountCountIsSameToBenefit = false;
            List<ServerData.T_Account> serverAccounts = new ServerData.T_Account().GetList();
            var query = db.Accounts;
            int accountCount = query.Count();
            if (accountCount > 0)
            {
                if (accountCount == serverAccounts.Count)
                {
                    accountCountIsSameToBenefit = true;
                }
            }
            return accountCountIsSameToBenefit;
        }


        #region 得到还没有在分析系统存在的账号
        /// <summary>
        /// 得到还没有在分析系统存在的账号
        /// </summary>
        /// <returns></returns>
        public List<Data.ServerData.T_Account> GetUnRegistAccounts(DB.DBManager db)
        {
            List<Data.ServerData.T_Account> accounts = new List<Data.ServerData.T_Account>();//保存还未注册的账户
            List<Data.ServerData.T_Account> serverAccounts = new Data.ServerData.T_Account().GetList();//获取所有账户
            foreach (Sys.Account account in db.Accounts)
            {
                var query = serverAccounts.Where(a => a.LoginAccount.Equals(account.AccountName)).ToList();
                if (query.Count > 0)
                {
                    if (query.First().Isdeleted == 1)
                    {
                        account.IsDeleted = true;
                    }
                }
            }
            db.SaveChanges();
            //   List<Sys.Account> benefitaccounts = new Sys.Account().GetList();
            foreach (Data.ServerData.T_Account t in serverAccounts)
            {

                var query = db.Accounts.Where(a => a.AccountName.Equals(t.LoginAccount));
                //   var query = from s in benefitaccounts where t.LoginAccount.Equals(s.AccountName) select s;
                if (query.Count() > 0)//曾经写入过
                {
                    if (query.First().IsDeleted == false && t.Isdeleted == 1)
                    {
                        query.First().IsDeleted = true;
                    }
                }
                else
                {
                    accounts.Add(t);
                }
            }

            db.SaveChanges();
            return accounts;
        }
        #endregion

        public Data.ServerData.T_Account GetModel(int id)
        {
            Data.ServerData.T_Account account = new T_Account();
            account.Id = id;
            account = (Data.ServerData.T_Account)Common.ADO.DBManager.GetModel(account);
            return account;
        }
    }
}